package tania.coca.ejercicios.equipos;

import javax.swing.table.DefaultTableModel;

public class Organizar {

	private DefaultTableModel model = null;

	public Organizar(DefaultTableModel model){

		this.model = model;


	}

	private void initModel(DefaultTableModel model, DefaultTableModel tempModel, int index){

		for (int i = 0; i < index; i++) {

			Object[] rows = {model.getValueAt(i, 0),model.getValueAt(i, 1),model.getValueAt(i, 2),model.getValueAt(i, 3),model.getValueAt(i, 4)};
			tempModel.addRow(rows);

		}

	}

	private void fillModel(DefaultTableModel model, DefaultTableModel tempModel, int index){

		for (int i = index; i < model.getRowCount(); i++) {

			Object[] rows = {model.getValueAt(i, 0),model.getValueAt(i, 1),model.getValueAt(i, 2),model.getValueAt(i, 3),model.getValueAt(i, 4)};
			tempModel.addRow(rows);

		}

	}

	public DefaultTableModel organizarArrayList(Object[] row){

		DefaultTableModel tempModel = new DefaultTableModel();
		tempModel.addColumn("Nombre Equipo");
		tempModel.addColumn("Ganados");
		tempModel.addColumn("Perdidos");
		tempModel.addColumn("AVG");
		tempModel.addColumn("Diferencia");

		int ganados = (Integer)row[1];
		int perdidos = (Integer)row[2];		

		for (int i = 0; i < model.getRowCount(); i++) {

			if(ganados > (Integer)model.getValueAt(i, 1)){					

				initModel(model, tempModel, i);
				tempModel.addRow(row);
				fillModel(model, tempModel, i);	
				break;

			}else if(ganados == (Integer)model.getValueAt(i, 1)){

				if(perdidos < (Integer)model.getValueAt(i, 2)){

					initModel(model, tempModel, i);
					tempModel.addRow(row);
					fillModel(model, tempModel, i);
					break;

				}else{

					tempModel.addRow(row);
					fillModel(model, tempModel, i);
					break;

				}

			}else if(ganados < (Integer)model.getValueAt(i, 1)){
				
				
				if(i == model.getRowCount()-1){
					
					fillModel(model, tempModel, 0);
					tempModel.addRow(row);

					
				}
				
				
			}

				
		}

		return tempModel;

	}

}
